const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
const gitRevisionPlugin = new GitRevisionPlugin()

module.exports = {
  apply: (compiler) => {
    compiler.hooks.emit.tapAsync("VersionPlugin", (compilation, callback) => {
      const startTime = Date.now();
      const version = gitRevisionPlugin.version();
      const hashCommit = gitRevisionPlugin.commithash();
      const versionInfo = {
        version,
        hashCommit,
      };
      compilation.assets["version.json"] = {
        source: () => JSON.stringify(versionInfo, null, 2),
        size: () => JSON.stringify(versionInfo).length,
      };
      const duration = Date.now() - startTime;
      console.log(`VersionPlugin: Processed assets in ${duration}ms`);
      callback();
    });
  },
};
